package com.crm.services;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.jdao.ITaskDAO;
import com.crm.model.mdao.CustomerMapper;
import com.crm.model.mdao.TaskMapper;
import com.crm.model.mpojo.Tasks;
import com.crm.model.pojo.Complaint;
import com.crm.model.pojo.Task;
import com.crm.model.pojo.Timeline;
import com.crm.util.LoginUtil;
import com.github.pagehelper.PageHelper;

/**
 * 待办任务
 * @author 梁箐
 *
 */
@Service
public class TaskServices {
	@Autowired
	ITaskDAO itaskeDao;
	@Autowired
	TaskMapper taskMapper;
	@Autowired
	TimeLineServices timeLineServices;
	@Autowired
	CustomerMapper customerMapper;
	
	/**
	 * 查询售后各表总数
	 * @return 
	 */
	public Map<String, String> AllTasksCount() {
		return taskMapper.AllTasksCount();
	}
	
	/**
	 * 销售各阶段跟踪
	 */
	public Map<String, String> allChance() {
		return taskMapper.allChance();
	}
	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectPage(int pageNum, int pageSize,Integer statueID,String description) {
		// 使用分页插件
		System.out.println("页码:" + pageNum + ",页数:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		Integer status = null;
		//对分类进行判断
		if (statueID!=null) {
			if (statueID==1427 || statueID==1428 || statueID==1429) {
				status=statueID;
			}
		}
		System.out.println("编号："+statueID+"\t分类："+status);
		// 获取记录数
		List<Map<String, String>> list = taskMapper.selectAllNew(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId(),status,description);
		System.out.println("list :"+list);
	    return list;
	}
	
	/**
	 * 查询所有(导出)
	 * @return
	 */
	public List<Map<String, String>> selectAllInfo() {
		return taskMapper.selectAllInfo(LoginUtil.getUser().getuDept(),LoginUtil.getUser().getuId());
	}
	
	/**
	 * 根据编号查询单条
	 * @param taskid
	 * @return
	 */
	public Map<String, String> selectOneByid(Integer taskid) {
		return taskMapper.selectByid(taskid);
	}

	//根据编号查询
	public Task findByid(Integer taskid) {
		return itaskeDao.findById(taskid).get();
	}

	/**
	 * 物理删除（更改状态）
	 * @return 
	 */
	public int deleteByStatue(Integer taskid) {
		return taskMapper.deleteByDeletestatue(taskid);
	}
	

	/**
	 * 添加待办任务
	 */
	public Task addTask(Task task) {
		Task save = itaskeDao.save(task);
		Timeline timeline=new Timeline();
		timeline.setTheme("待办任务");
		timeline.setTimes(new Date());
		timeline.setCustId(save.getCustomer().getCustId());
		timeline.setDetails("客户："+save.getCustomer().getCustName()+" 待办任务为:--"+save.getTaskDescription());
		timeLineServices.insertTimeLine(timeline);
		System.out.println(save);
		return save;
	}
	
	/**
	 * 添加待办任务(根据关怀)
	 */
	public int addTasks(Tasks tasks) {
		int insert = taskMapper.insert(tasks);
		Timeline timeline=new Timeline();
		timeline.setTheme("待办任务");
		timeline.setTimes(new Date());
		timeline.setCustId(tasks.getCustId());
		Map<String, String> selectById = customerMapper.selectById(tasks.getCustId());
		timeline.setDetails("客户："+selectById.get("cust_name")+" 待办任务为:--"+tasks.getTaskDescription());
		timeLineServices.insertTimeLine(timeline);
		return insert;
	}
	
	/**
	 * 更新待办任务
	 * @param tasks
	 * @return
	 */
	public int updateTasks(Tasks tasks) {
		return	taskMapper.updateByPrimaryKeySelective(tasks);
	}
	
	
}
